$(window).load(function() {
	$("#submitBtn").button();
	$("#submitBtn").click(function() {
		$('#begin').val(0);
		search();
	});
	
	$("#conflink").button({icons: {primary:'ui-icon-gear'}});
	$("#conflink").click(function() {
		window.location.href="confModule.do";
	});;

	$('#columnList').dialog({
		autoOpen : false,
		width : 800,
		height : 460,
		buttons : {
			"Ok" : function() {
				$(this).dialog("close");
			},
			"Cancel" : function() {
				$(this).dialog("close");
			}
		}
	});
	
	$('#tabs').tabs();

//	$("#conflink, ul#pageBarUl li").hover(function() {
//		$(this).addClass('ui-state-hover');
//	}, function() {
//		$(this).removeClass('ui-state-hover');
//	});

	$("#a-firstpage").click(function() {
		$("#begin").val("0");
		search();
	});
	
	$("#a-prevpage").click(function() {
		if (Number($("#begin").val()) <= 0) {
			alert("已经是第一页了");
		} else {
			var newBegin = Number($("#begin").val()) - Number($("#length").val());
			if (newBegin < 0) {
				newBegin = 0;
			}
			$("#begin").val(newBegin);
			search();
		}
	});
	
	$("#a-nextpage").click(function() {
		if (Number($("#begin").val()) + Number($("#length").val()) >= Number($("#count").val())) {
			alert("已经是最后一页了");
		} else {
			var newBegin = Number($("#begin").val()) + Number($("#length").val());
			if (newBegin < 0) {
				newBegin = 0;
			}
			$("#begin").val(newBegin);
			search();
		}
	});
	
	$("#a-endpage").click(function() {
		var newbegin =  (Math.ceil(Number($("#count").val())/Number($("#length").val())) - 1) * Number($("#length").val());
//		var newbegin = Number($("#count").val()) - Number($("#length").val());
		if (newbegin < 0) {
			newbegin = 0;
		}
		$("#begin").val(newbegin);
		search();
	});
	
	$("#a-playpage").click(function() {
		var current = Number($("#vcurrent").val());
		if (current > 0 && current <= Math.ceil(Number($("#count").val())/Number($("#length").val()))) {
			var newbegin = (current - 1) * Number($("#length").val());
			$("#begin").val(newbegin);
			search();
		} else {
			alert("输入不合法");
		}
		
	});

	$('#moduleName').change(
			function() {
				$("#moduleName option").each(
						function(i, o) {
							if ($(this).attr("selected")) {
								var param = this.value;
								$('#tableName').empty();
								if (param != "全部") {
									$.ajax({
										type : 'POST',
										contentType : 'application/json',
										url : 'getTableList.do',
										data : param,
										dataType : 'json',
										success : function(data) {
											var sel = $('#tableName');
											sel.append('<option>全部</option>');
											$.each(data, function(i, n) {
												sel.append('<option value="'
														+ n.tableName + '">'
														+ n.tableName
														+ '</option>');
											});
										}
									// error : failed
									});
								}
							}
						});
			});

	$.ajax({
		type : 'POST',
		contentType : 'application/json',
		url : 'getModuleList.do',
		data : '',
		dataType : 'json',
		success : function(data) {
			var sel = $('#moduleName');
			$.each(data, function(i, n) {
				sel.append('<option value="' + n.moduleId + '">' + n.moduleName
						+ '</option>');
			});
		}
	// error : failed
	});
	
	var tableCommentDblclick = function(a, b) {
		var text = a.text();
		a.empty();
		var input = $("<input type='text' />");
		input.val(text);
		input.blur(function() {
			var newtext = $(this).val();
			var param = JSON.stringify({'tableName':b,'comment': newtext});
			$.ajax({
				type : 'POST',
				contentType : 'application/json',
				url : 'updateTableComment.do',
				data : param,
				dataType : 'json',
				success : function(data) {
					if (data.success = true) {
						a.empty();
						a.text(newtext);
					} else {
						a.empty();
						a.text(text);
						alert("失败");
					}
				},
				error : function() {
					a.empty();
					a.text(text);
					alert("失败");
				}
			});
		});
		input.keydown(function(event) {
			if (event.keyCode == 13) {
				input.trigger("blur");
			}
		});
		input.dblclick(function(event) {
			event.stopPropagation();
		});
		input.appendTo(a);
		input.focus();
	};
	
	var search = function() {
		var elemUserinfo = $('#viewdictPageItem');
		var userinfo = elemUserinfo.serializeObject();
		var jsonuserinfo = JSON.stringify(userinfo);

		$.ajax({
			type : 'POST',
			contentType : 'application/json',
			url : 'getViewList.do',
			data : jsonuserinfo,
			dataType : 'json',
			success : function(data) {
				$("tr#ready").remove();
				$.each(data.viewList, function(i, n) {
					var row = $("<tr></tr>");
					var cell1 = $("<td class='ui-widget-content'>" + (i + 1) + "</td>");
					cell1.appendTo(row);
					var cell2 = $("<td class='ui-widget-content'>" + n.tableName + "</td>");
					cell2.click(function(){
						showTableColumn(n.tableName);
						showIndex(n.tableName);
						$('#columnList').dialog('open');
					});
					cell2.appendTo(row);
					var cell3 = $("<td class='ui-widget-content'>" + n.tableComment + "</td>");
					cell3.dblclick(function() {
						tableCommentDblclick($(this), n.tableName);
					});
					cell3.attr("title", "双击修改");
					cell3.appendTo(row);
					row.attr("id", "ready");
					row.appendTo("#datas");
				});
//				$( "#datas" ).selectable();
				$("#vcurrent").val(data.page.current);
				$("#count").val(data.page.count);
				$("#vtotal").empty();
				$("#vtotal").append(data.page.total);
				;
			},
			error : function() {alert("失败");}
		});
	};
	
	var colCommentDblclick = function(a, b, c) {
		var text = a.text();
		a.empty();
		var input = $("<input type='text' />");
		input.val(text);
		input.blur(function() {
			var newtext = $(this).val();
			var param = JSON.stringify({'tableName':b, 'columnName':c, 'comment': newtext});
			$.ajax({
				type : 'POST',
				contentType : 'application/json',
				url : 'updateColumnComment.do',
				data : param,
				dataType : 'json',
				success : function(data) {
					if (data.success = true) {
						a.empty();
						a.text(newtext);
					} else {
						a.empty();
						a.text(text);
						alert("失败");
					}
				},
				error : function() {
					a.empty();
					a.text(text);
					alert("失败");
				}
			});
		});
		input.keydown(function(event) {
			if (event.keyCode == 13) {
				input.trigger("blur");
			}
		});
		input.dblclick(function(event) {
			event.stopPropagation();
		});
		input.appendTo(a);
		input.focus();
	};
	
	var showTableColumn = function(tableName) {
		var param = tableName;

		$.ajax({
			type : 'POST',
			contentType : 'application/json',
			url : 'getTableColumn.do',
			data : param,
			dataType : 'json',
			success : function(data) {
				$("table#colDatas tr#ready").remove();
				$.each(data, function(i, n) {
					var row = $("<tr></tr>");
					var cell1 = $("<td class='ui-widget-content'>" + n.columnName + "</td>");
					cell1.appendTo(row);
					var cell2 = $("<td class='ui-widget-content'>" + n.dataType + "</td>");
					cell2.appendTo(row);
					var cell3 = $("<td class='ui-widget-content'>" + n.dataLength + "</td>");
					cell3.appendTo(row);
					var cell4 = $("<td class='ui-widget-content'>" + n.colComment + "</td>");
					cell4.dblclick(function() {
						colCommentDblclick($(this), n.tableName, n.columnName);
					});
					cell4.attr("title", "双击修改");
					cell4.appendTo(row);
					row.attr("id", "ready");
					row.appendTo("#colDatas");
				});
			},
			error : function() {alert("失败");}
		});
	};
	
	var showIndex = function(tableName) {
		var param = tableName;
		
		$.ajax({
			type : 'POST',
			contentType : 'application/json',
			url : 'findIndexByTable.do',
			data : param,
			dataType : 'json',
			success : function(data) {
				$("table#indexDatas tr#ready").remove();
				$.each(data, function(i, n) {
					var row = $("<tr></tr>");
					var cell1 = $("<td class='ui-widget-content'>" + n.indexName + "</td>");
					cell1.appendTo(row);
					var cell2 = $("<td class='ui-widget-content'>" + n.columnName + "</td>");
					cell2.appendTo(row);
					var cell3 = $("<td class='ui-widget-content'>" + n.columnPosition + "</td>");
					cell3.appendTo(row);
					var cell4 = $("<td class='ui-widget-content'>" + n.columnLength + "</td>");
					cell4.appendTo(row);
					row.attr("id", "ready");
					row.appendTo("#indexDatas");
				});
			},
			error : function() {alert("失败");}
		});
	};
});